package com.jb.repair.acticity;

import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jb.repair.R;
import com.jb.repair.common.AppConfig;
import com.jb.repair.dao.Db;
import com.jb.repair.dao.ModifyDb;
import com.jb.repair.model.YcjlModel;
import com.jb.repair.util.DownAttachmentDialogUtil;
import com.jb.repair.util.DownFileAsyncTask;
import com.jb.repair.util.IntentUtils;
import com.jb.repair.util.MessageUtil;
import com.jb.repair.util.StringUtils;
import com.jb.repair.view.InfoTextView;
import com.jb.repair.view.TitleView;

import java.io.File;

/**
 * 异常
 */
public class ShowChangeActivity extends BaseActivity {
    //设备名称 巡检项目 异常发生时间 异常说明 附件
    private TextView tv_devname, tv_itemname, tv_happen_date, tv_explain;
    private InfoTextView iv_iv;
    private YcjlModel ycjlModel;
    private Db db;

    private TitleView titleView;
    private Boolean isModify = false;
    private EditText et;
    private String imgPath;
    private DownAttachmentDialogUtil dialogUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_change);
        db = Db.getInstance(this);
        initViews();
        ycjlModel = (YcjlModel) getIntent().getSerializableExtra("ycjl");
        //  String name = db.getName("eq_name", "TB_EQ_BASICINFO", "guid", ycjlModel.eq_id);
        tv_devname.setText(ycjlModel.eq_name);
//        tv_itemname.setText(ycjlModel.eq_id);
        tv_happen_date.setText(ycjlModel.found_time);
        tv_explain.setText(ycjlModel.abn_describe);
        imgPath = AppConfig.UPLOAD_PATH + ycjlModel.attachment + ".jpg";
        File f = new File(imgPath);
        if (!f.exists()) {
            imgPath = AppConfig.ATTACHMENT_PATh + ycjlModel.attachment + ".jpg";
        }
        iv_iv.setImagePath(imgPath);

        String name2 = db.getName("WJMC", "TB_FILE_LB", "OBJ_ID", ycjlModel.attachment);
        iv_iv.setText(name2);
        iv_iv.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                Log.i("点击测试", "收到点击事件");
                //附件的guid
                final String guid;
                //附件的文件类型
                final String fjlx;

                    guid = db.getName("GUID", "TB_FILE_LB", "OBJ_ID", ycjlModel.attachment);
                    fjlx = db.getName("LX", "TB_FILE_LB", "OBJ_ID", ycjlModel.attachment);

                //判断是否有附件
                boolean nullOrEmpty = StringUtils.isNullOrEmpty(iv_iv.getText());
                Log.e("LLL", String.valueOf(nullOrEmpty));
                if (nullOrEmpty) {
                    return;
                } else {
                    //有附件
                    String filePath = AppConfig.ATTACHMENT_PATh + guid + "." + fjlx;
                    Log.e("1111", filePath);
                    File file = new File(filePath);
                    if (!file.exists()) {
                        Toast.makeText(ShowChangeActivity.this, "文件不存在", Toast.LENGTH_SHORT).show();
                        dialogUtil = new DownAttachmentDialogUtil(ShowChangeActivity.this, R.style.myDialog,
                                new DownAttachmentDialogUtil.AttachDialogListener() {
                                    @Override
                                    public void onClick(View v) {
                                        switch (v.getId()) {
                                            case R.id.attach_dialog_canlebtn:
                                                dialogUtil.dismiss();
                                                break;
                                            case R.id.attach_dialog_surebtn:
                                                new DownFileAsyncTask(ShowChangeActivity.this, guid, ycjlModel.attachment, fjlx).execute();
                                                dialogUtil.dismiss();
                                                break;
                                        }
                                    }

                                });
                        Window win = dialogUtil.getWindow();
                        win.setGravity(Gravity.CENTER);
                        dialogUtil.show();
                    } else {
                        IntentUtils.JumpToIntent(ShowChangeActivity.this, fjlx, filePath);

                    }

                }
            }


        });


    }

    public void initViews() {
        tv_devname = (TextView) findViewById(R.id.tv_devname);
//        tv_itemname = (TextView) findViewById(R.id.tv_itemname);
        tv_happen_date = (TextView) findViewById(R.id.tv_happen_date);
        tv_explain = (TextView) findViewById(R.id.tv_explain);
        iv_iv = (InfoTextView) findViewById(R.id.iv_iv);
        et = (EditText) findViewById(R.id.et_explain);

        titleView = (TitleView) findViewById(R.id.titleView);
        titleView.setOnClickListener(new TitleView.OnTitleViewClickListener() {
            @Override
            public void onLeftImageClick(View view) {
                super.onLeftImageClick(view);
                if (isModify) {
                    isModify = false;
                    titleView.setRightTextView("修改");
                    tv_explain.setVisibility(View.VISIBLE);
                    et.setVisibility(View.GONE);
                } else {
                    finish();
                }
            }

            @Override
            public void onRightTextClick(View view) {
                super.onRightTextClick(view);
                if (isModify) {
                    titleView.setRightTextView("修改");
                    tv_explain.setVisibility(View.VISIBLE);
                    et.setVisibility(View.GONE);
                    if (et.getText().toString().trim().endsWith(ycjlModel.abn_describe)) {
                        MessageUtil.showToast(getApplicationContext(), "无修改");
                    } else {
                        ModifyDb.getInstance(getApplicationContext()).modifyResult(ycjlModel.result_id, et.getText().toString().trim(), true);
                        tv_explain.setText(et.getText().toString());
                        MessageUtil.showToast(getApplicationContext(), "修改成功");
                    }
                } else {
                    titleView.setRightTextView("保存");
                    tv_explain.setVisibility(View.GONE);
                    et.setVisibility(View.VISIBLE);
                    et.setText(ycjlModel.abn_describe);
                }
                isModify = !isModify;
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (isModify) {
            isModify = false;
            titleView.setRightTextView("修改");
            tv_explain.setVisibility(View.VISIBLE);
            et.setVisibility(View.GONE);
        } else {
            finish();
        }
    }
}
